package org.snaker.engine.parser;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public abstract class AbstractNodeParserProvider<T> implements NodeParserProvider<T> {
    private final Map<String, NodeParser<T>> parsers = new HashMap<>();

    @Override
    public NodeParser<T> getByName(String name) {
        return  Optional.ofNullable(parsers.get(name)).orElseThrow(() -> new RuntimeException("no parser found for name: " + name));
    }

    @Override
    public NodeParserProvider<T> addProvider(String name, NodeParser<T> parser) {
        parsers.put(name, parser);
        return this;
    }
}
